package org.mspring.mlog.api.mail;

import org.mspring.mlog.service.OptionService;
import org.mspring.platform.mail.MailSenderConfig;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.utils.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @since 2014-03-28 13:43:52
 * @author Gao Youbo
 */
@Component(value = "mailConfig")
public class MailConfig implements MailSenderConfig {

    private static final int DEFAULT_PORT = 25;
    private static final String DEFAULT_ENCODING = "UTF-8";

    @Autowired
    private OptionService optionService;

    @Override
    public String getHost() {
        return optionService.getOption("smtp_host");
    }

    @Override
    public String getFrom() {
        return optionService.getOption("mail_from");
    }

    @Override
    public String getUsername() {
        return optionService.getOption("smtp_username");
    }

    @Override
    public String getPassword() {
        return optionService.getOption("smtp_password");
    }

    @Override
    public int getPort() {
        String str = optionService.getOption("smtp_port");
        if (StringUtils.isBlank(str) || !ValidatorUtils.isInteger(str)) {
            return DEFAULT_PORT;
        }
        return Integer.parseInt(str.trim());
    }

    @Override
    public boolean getSSLEnable() {
        String smtp_ssl_enable = optionService.getOption("smtp_ssl_enable");
        if (StringUtils.isNotBlank(smtp_ssl_enable) && smtp_ssl_enable.equals("true")) {
            return true;
        }
        return false;
    }

    @Override
    public String getEncoding() {
        return DEFAULT_ENCODING;
    }

}
